package servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import factory.DAOFactory;
import vo.Menu;

/**
 * Servlet implementation class UpdateMenuServlet
 */
@WebServlet(description = "更新菜品", urlPatterns = { "/UpdateMenuServlet" })
public class UpdateMenuServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ServletConfig servletConfig;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateMenuServlet() {
		super();

	}

	public void init(ServletConfig config) throws ServletException {
		this.servletConfig = config;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 新建一个SmartUpload对象
		SmartUpload su = new SmartUpload();
		// 上传初始化
		su.initialize(servletConfig, request, response);
		// 限制每个上传文件的最大长度
		su.setMaxFileSize(1000000);
		try {
			su.setDeniedFilesList("exe,bat,jsp,htm,html");
		} catch (ServletException | IOException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 上传文件
		try {
			su.upload();
		} catch (ServletException | IOException | SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 获取上传的文件操作
		Files files = su.getFiles();
		// 获取单个文件
		File singleFile = files.getFile(0);
		// 获取上传文件的扩展名
		String fileType = singleFile.getFileExt();
		// 设置上传文件的扩展名
		String[] type = { "JPG", "jpg" };
		// 判断上传文件的类型是否正确
		int place = java.util.Arrays.binarySearch(type, fileType);
		// 判断文件扩展名是否正确
		if (place != -1) {
			// 判断该文件是否被选择
			if (!singleFile.isMissing()) {
				// 以系统时间作为上传文件名称，设置上传完整路径
				String fileName = String.valueOf(System.currentTimeMillis());
				String filedir = "images/" + fileName + "." + singleFile.getFileExt();
				// 执行上传操作
				try {
					singleFile.saveAs(filedir, File.SAVEAS_VIRTUAL);
				} catch (IOException | SmartUploadException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("上传至： " + filedir);
				String menuid = request.getParameter("id");
				int menuid1 = Integer.valueOf(menuid);
				String menuName = su.getRequest().getParameter("menuName");
				String menuPrice = su.getRequest().getParameter("menuPrice");
				String menuNotice = su.getRequest().getParameter("menuNotice");
				Menu menu = new Menu();
				menu.setMenu_id(menuid1);
				menu.setMenu_name(menuName);
				menu.setMenu_content(menuNotice);
				Double price = Double.parseDouble(menuPrice);
				menu.setMenu_price(price);
				menu.setMenu_image(fileName);
				if (DAOFactory.getMenuDAOInstance().update(menu) > 0) {
					response.sendRedirect("store_index.jsp");
				}
				else {
					response.setContentType("text/html; charset=UTF-8");
					response.getWriter().println("<script LANGUAGE='javascript'> alert('修改失败！');</script>");
				}
			}
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
